﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model = BLL.Model;


namespace DAL.Translator
{
    public class OrderItemTranslator
    {
        public static DbEntity.OrderItem TranslateBusinessToDbEntity(Model.OrderItem request,DbEntity.OrderContainer container)
        {
            DbEntity.OrderItem response = null;
            if (request != null)
            {
                if (request.OrderItemId > 0)
                {
                    response = GetOrderItemById(request.OrderItemId,container);
                    response.TotalPrice = request.TotalPrice;
                    response.Quantity = request.Quantity;
                    response.ProductReference.EntityKey = new System.Data.EntityKey("OrderContainer.Products", "ProductId", request.Product.ProductId);
                }
                else
                {
                    response = new DbEntity.OrderItem();
                    response.OrderItemId = request.OrderItemId;
                    response.TotalPrice = request.TotalPrice;
                    response.Quantity = request.Quantity;
                    response.ProductReference.EntityKey = new System.Data.EntityKey("OrderContainer.Products", "ProductId", request.Product.ProductId);
                }
            }
            return response;
        }

        private static DbEntity.OrderItem GetOrderItemById(int orderItemId, DbEntity.OrderContainer container)
        {
            DbEntity.OrderItem orderItem = null;
            orderItem = (from item in container.OrderItems where item.OrderItemId == orderItemId select item).FirstOrDefault<DbEntity.OrderItem>();
            return orderItem;
        }
    }
}
